package com.base.caselib.presenter;

import android.app.Activity;
import android.os.Environment;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.caselib.bean.CarInfo;
import com.base.caselib.bean.CarType;
import com.base.caselib.view.VehicleUpView;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.http.CallBackFroPark;
import com.base.commonlib.http.FileBean;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.ResponseT;
import com.base.commonlib.utils.BitmapUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * Created by weikailiang on 2020/2/2.
 */

public class VehicleUpPresenter extends BasePresenter<VehicleUpView> {

    public VehicleUpPresenter(VehicleUpView view, Activity activity) {
        super(view, activity);
    }

    private List<CarType> mCarTypes = new ArrayList<>();
    CarType mCarType = null;
    public void getCarType(){
        //小型汽车、大型汽车、教练汽车、大型新能源汽车、小型新能源汽车、其他号牌
        mCarType = new CarType();
        mCarType.setDictId("0");
        mCarType.setDictValue("小型汽车");
        mCarTypes.add(mCarType);
        mCarType = new CarType();
        mCarType.setDictId("1");
        mCarType.setDictValue("大型汽车");
        mCarTypes.add(mCarType);
        mCarType = new CarType();
        mCarType.setDictId("2");
        mCarType.setDictValue("教练汽车");
        mCarTypes.add(mCarType);
        mCarType = new CarType();
        mCarType.setDictId("3");
        mCarType.setDictValue("大型新能源汽车");
        mCarTypes.add(mCarType);
        mCarType = new CarType();
        mCarType.setDictId("4");
        mCarType.setDictValue("小型新能源汽车");
        mCarTypes.add(mCarType);
        mCarType = new CarType();
        mCarType.setDictId("5");
        mCarType.setDictValue("其他号牌");
        mCarTypes.add(mCarType);
        mView.returnSpinnerCarType(mCarTypes);
    }
    public int getTypePosition(String type) {
        if (mCarTypes != null) {
            for (int i = 0; i < mCarTypes.size(); i++) {
                if (mCarTypes.get(i).getDictValue().equals(type)) {
                    return i;
                }
            }
        }
        return 0;
    }

    public void up(final CarInfo info){
        mView.showPb_mes("正在压缩图片。。。");
        List<String> paths = new ArrayList<>();
        if (info.getCarFiles()!=null) {
            for (String fils : info.getCarFiles()) {
                paths.add(BitmapUtil.compressImage(fils));
            }
        }

        List<String> paths1 = new ArrayList<>();
        if (info.getJszFiles()!=null) {
            for (String fils : info.getJszFiles()) {
                paths1.add(BitmapUtil.compressImage(fils));
            }
        }
        List<String> paths2 = new ArrayList<>();
        if (info.getXszFiles()!=null) {
            for (String fils : info.getXszFiles()) {
                paths2.add(BitmapUtil.compressImage(fils));
            }
        }
        List<String> paths3 = new ArrayList<>();
        if (info.getSfzFiles()!=null) {
            for (String fils : info.getSfzFiles()) {
                paths3.add(BitmapUtil.compressImage(fils));
            }
        }
        //压缩完成
        mView.hidePb_mes();

        mView.showPb_mes("正在保存数据");
        final List<FileBean> mFileList = new ArrayList<>();
        FileBean mFileBean = null;
        for (String str : paths){
            mFileBean = new FileBean("carFiles",str);
            mFileList.add(mFileBean);
        }
        for (String str : paths1){
            mFileBean = new FileBean("jszFiles",str);
            mFileList.add(mFileBean);
        }

        for (String str : paths2){
            mFileBean = new FileBean("sfzFiles",str);
            mFileList.add(mFileBean);
        }
        for (String str : paths3){
            mFileBean = new FileBean("xszFiles",str);
            mFileList.add(mFileBean);
        }

        Disposable enqueue = RealNetWorkUtil.postPics().method("api-statistics/check/save").filePaths(mFileList)
                .addParams("carType",info.getCarType())
                .addParams("carNo",info.getCarNo())
                .addParams("outWay",info.getOutWay())
                .addParams("description",info.getDescription())
                .addParams("phone",info.getPhone())
                .addParams("checkPoint",info.getCheckPoint())
                .addParams("areaContact",info.getAreaContact())
                .addParams("febrileState",info.getFebrileState())
                .addParams("peoples",info.getPeoples())
                .addParams("inWay",info.getInWay())
                .build().enqueueForUpFiles(new CallBackFroPark<ResponseT>() {
                    @Override
                    public void onResponse(ResponseT responseT) throws Exception {
                        mView.hidePb_mes();
                        mView.showMes(responseT.getData().toString());
                        for (FileBean fileBean : mFileList){
                            File f = new File(fileBean.getValue());
                            if (f.exists()){
                                f.delete();
                            }
                        }
                        BaseApp.setHistoryText(mUser.getUserId(),info.getCheckPoint());
                        //自定义
                        ARouter.getInstance().build("/lpark/memory")
                                .withString("ResultActivity", "/crpuct/vehicleup")
                                .withString("PATH", Environment.getExternalStorageDirectory().getAbsolutePath() + "/lpark/park")
                                .navigation();
                        mActivity.finish();
                    }

                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(ResponseT responseT) {
                            mView.hidePb_mes();
                    }
                });

        mCompositeDisposable.add(enqueue);
    }



}
